'use strict';
const dayjs = require("dayjs");
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, TEXT, BIGINT } = app.Sequelize;
    const Post = app.model.define(
        'Post',
        {
            id: {type: BIGINT, primaryKey: true, autoIncrement: true},
            user_id: {type: BIGINT, allowNull: false},
            title: {type: STRING(100), allowNull: true},
            content: {type: TEXT, allowNull: false},
            images: {type: STRING(1000), defaultValue: null},
            status: {type: TINYINT, defaultValue: -1},
            creator: {type: BIGINT, defaultValue: 0},
            updater: {type: BIGINT, defaultValue: 0},
            deleted: {type: TINYINT, defaultValue: 0},
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'post',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    Post.associate = function () {
        app.model.Business.Post.belongsTo(app.model.Business.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user'
        });
        // 有许多Likes和评论
        app.model.Business.Post.hasMany(app.model.Business.PostLike, {as: 'likes', foreignKey: 'post_id'});
        app.model.Business.Post.hasMany(app.model.Business.PostComment, {as: 'comments', foreignKey: 'post_id'});
    };
    return Post
};
